package com.gosophia.reportData.action;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.holidayManagement.ws.HolidayManagementWebService;
import com.gosophia.reportData.ws.ReportJobWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;

/**
 * 查看数据报送任务
 * 
 * @author zhao_yanfang
 * 
 */
@Component
@Scope("prototype")
public class ShowReportJobListAction extends BaseAjaxAction<JobDTO> {

	@Autowired
	private ReportJobWebService reportJobWebService;
    @Autowired
    private HolidayManagementWebService holidayManagementWebService;
    private String remind;
    private String warn;
    private String noState;
	/**
	 * 查询数据上报任务
	 */
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		// 高级查询
		if (searchType) {
            if("0".equals(remind) && "1".equals(warn) && noState==null){
                //提醒
                Date date = holidayManagementWebService.findTheAfterDay(new Date(), 2);
                PropertyFilter isNull = new PropertyFilter();
                isNull.setPropertyName("actualEndTime");
                isNull.setOperator(OperatorType.IS_NULL);
                PropertyFilter scheduleGE = new PropertyFilter("scheduleEndDate",OperatorType.GE,new Date());
                PropertyFilter scheduleLE = new PropertyFilter("scheduleEndDate",OperatorType.LE,date);
                isNull.and(scheduleGE.and(scheduleLE));
                //预警
                PropertyFilter p  = new PropertyFilter("actualEndTime",OperatorType.GT,"${scheduleEndDate}");
                PropertyFilter isNull2 = new PropertyFilter();
                isNull2.setPropertyName("actualEndTime");
                isNull2.setOperator(OperatorType.IS_NULL);
                p.or(isNull2.and(new PropertyFilter("scheduleEndDate",OperatorType.LT,new Date())));
                //或
                isNull.or(p);
                propertyFilters.add(isNull);
            }
            if("0".equals(remind) && warn==null && noState==null){
                Date date = holidayManagementWebService.findTheAfterDay(new Date(), 2);
                PropertyFilter isNull = new PropertyFilter();
                isNull.setPropertyName("actualEndTime");
                isNull.setOperator(OperatorType.IS_NULL);
                PropertyFilter scheduleGE = new PropertyFilter("scheduleEndDate",OperatorType.GE,new Date());
                PropertyFilter scheduleLE = new PropertyFilter("scheduleEndDate",OperatorType.LE,date);
                isNull.and(scheduleGE.and(scheduleLE));
                propertyFilters.add(isNull);
            }
            if("1".equals(warn) && remind==null && noState==null){
                PropertyFilter p  = new PropertyFilter("actualEndTime",OperatorType.GT,"${scheduleEndDate}");
                PropertyFilter isNull2 = new PropertyFilter();
                isNull2.setPropertyName("actualEndTime");
                isNull2.setOperator(OperatorType.IS_NULL);
                p.or(isNull2.and(new PropertyFilter("scheduleEndDate",OperatorType.LT,new Date())));
                propertyFilters.add(p);
                
            }
			this.page = reportJobWebService.findReportJobADVByPage(page
					.getPageNo(), page.getPageSize(), propertyFilters,
					orderConditions, userInfoId);
		} else {
			if (searchString != null && searchString.length() > 0) {
				if (propertyFilters == null) {
					propertyFilters = new ArrayList<PropertyFilter>();
				}
				PropertyFilter missionCode = new PropertyFilter(
						"mission.missionCode", OperatorType.LIKE, searchString);
				PropertyFilter missionName = new PropertyFilter(
						"mission.missionName", OperatorType.LIKE, searchString);
				PropertyFilter launchErs = new PropertyFilter(
						"mission.firstJob.operator.userName", OperatorType.LIKE,
						searchString);
				PropertyFilter propertyFilter = missionCode.or(missionName).or(
						launchErs);
				propertyFilters.add(propertyFilter);
				this.page = reportJobWebService.findReportJobByPage(
						searchString, page.getPageNo(), page.getPageSize(),
						propertyFilters, orderConditions, userInfoId);
			}
			this.page = reportJobWebService.findReportJobByPage(searchString,
					page.getPageNo(), page.getPageSize(), propertyFilters,
					orderConditions, userInfoId);
		}
		return SUCCESS;
	}

	public JobDTO getDetailData() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getRemind() {
		return remind;
	}

	public void setRemind(String remind) {
		this.remind = remind;
	}

	public String getWarn() {
		return warn;
	}

	public void setWarn(String warn) {
		this.warn = warn;
	}

	public String getNoState() {
		return noState;
	}

	public void setNoState(String noState) {
		this.noState = noState;
	}
	

}
